package com.lph.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lph.dao.EnterpriseDaoI;
import com.lph.dao.MenPropertiesDaoI;
import com.lph.dao.MenberDaoI;
import com.lph.model.Enterprise;
import com.lph.model.MenProperties;
import com.lph.model.Menber;
import com.lph.model.Msg;
import com.lph.util.Ch2PinYin;
import com.lph.util.EmailUtil;
import com.lph.util.MD5Util;

@Service
public class MenberService implements BaseServiceI<Menber>{
	
	@Autowired
	private MenberDaoI mdi;
	@Autowired
	private MenPropertiesDaoI mpdi;
	@Autowired
	private EnterpriseDaoI edi;

	
	
	public List<Menber> find(Menber t) {
		List<Menber> newList=new ArrayList<Menber>();
		List<Menber> list=mdi.find(t);
		
		for (Menber menber : list) {
			//查询会员性质
			MenProperties mp = new MenProperties();
			mp.setMenId(menber.getId());
			List<MenProperties> mps = mpdi.find(mp);
			menber.setMps(mps);
			//查询企业信息
			Enterprise emp = new Enterprise();
			emp.setMenId(menber.getId());
			List<Enterprise> emps = edi.find(emp);
			menber.setEmps(emps);
			
			//重新添加到新的集合中
			newList.add(menber);
		}
		return newList;
	}

	public Menber get(Menber t) {
		List<Menber> list=this.find(t);
		return list.size()>0?list.get(0):null;
	}

	public Msg edit(Menber t) {
		int i=mdi.edit(t);
		Msg m=new Msg("更新失败", false);
		if (i>0) {
			m.setMsg("更新成功");
			m.setIs(true);
		}
		return m;
	}

	public Msg batchRemove(String ids) {
		String rids[]=ids.split(",");
		List<String> list=new ArrayList<String>();
		list=Arrays.asList(rids);//数组转换集合
		int i=mdi.batchRemove(list);
		Msg m=new Msg("删除失败", false);
		if (i>0) {
			m.setMsg("删除成功");
			m.setIs(true);
		}
		return m;
	}


	public Msg remove(Integer id) {
		int i=mdi.remove(id);
		Msg m=new Msg("删除失败", false);
		if (i>0) {
			m.setMsg("删除成功");
			m.setIs(true);
		}
		return m;
	}

	public Msg insert(Menber t) {
		//设置登录名
		String loginName=Ch2PinYin.getFirstChpy(t.getName())+new SimpleDateFormat("MMdd").format(t.getBirthday());
		
		//密码
		String password=MD5Util.getMd5( t.getIdcard().substring(t.getIdcard().length()-6));
		t.setLoginName(loginName);
		t.setPassword(password);
		int i = mdi.insert(t);
		Msg m = new Msg("插入失败", false);
		if(i>0){
			m.setMsg("插入成功");
			m.setIs(true);
		}
		return m;
	}

	public Msg batchInsert(String objs) {
		// TODO Auto-generated method stub
		return null;
	}

	

	public Msg sendEmail(String loginName,HttpServletRequest req){
		Msg m=new Msg();
		Menber mb=new Menber();
		mb.setLoginName(loginName);
		
		mb=this.get(mb);
		if(mb==null){
			m=new Msg("邮件发送失败", false);
					
		}else{
			String to=mb.getEmail();
			if(to==null||to==""){
				m=new Msg("邮件发送失败，用户邮箱不存在，请联系管理员18878629614", false);
			}else{
				Calendar c=Calendar.getInstance();
				Random r=new Random();
				c.add(Calendar.MINUTE, 30);
				String code=(r.nextInt(9000)+1000)+"";
				mb.setCode(code);
				mdi.edit(mb);
				//req.getSession().setAttribute("pwdCode", code);
				String token="?login="+loginName+"&ms"+c.getTimeInMillis()+"&token="+MD5Util.getMd5(code);
				String com="此链接只能用于'商会后台系统'密码找回,点击该链接"+"<a href='#'>"+"http://localhost:8082/"+req.getContextPath()+"/sc/editPwd.coc"+
						token+"</a>";
				
				System.out.println("com"+com);//输出连接
				boolean b=EmailUtil.sendEmsil(to, com);
				if(b){
					m=new Msg("邮件发送成功",true,mb);
				}else{
					m=new Msg("邮件发送失败",false);
				}
			}
		}
		return m;
		
	}
	
	public Msg editPwd(HttpServletRequest req,String ...str){
		Msg m=new Msg("验证成功",true);
		/*//判断时间是否有效
				String ms=str[1]==null?"0":str[1];
				ms=ms.replaceAll("[^0-9]+", "");
				long mins=Long.parseLong(ms);
				if(System.currentTimeMillis()>mins){
					m=new Msg("链接时间已过，无法修改，请重新获取链接!",false);
					return m;
				}
		//获取token
*/		String token=str[2];
		//获取session的验证码
		//String code=req.getSession().getAttribute("pwdCode")+"";
		Menber mb=new Menber();
		mb.setLoginName(str[0]);
		mb=this.get(mb);
		if(mb==null){
			m = new Msg("链接内容被非法修改，无法修改，请重新获取链接!", false);
			return m;
		}
		String code=mb.getCode();
		
		System.out.println("="+code);
		
		if(!MD5Util.getMd5(code).equals(token)){
			m=new Msg("token错误，无法修改，请重新获取连接",false);
			return m;
		}
		//销毁session
		//req.getSession().removeAttribute("pwdCode");
		
		 mb=new Menber();
		mb.setLoginName(str[0]);
		m.setObject(mb);
		return m;
		
	}
	
}
